﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-                                           *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019年5月23日 11:55:35
 *  文件描述:截图保存
 *****************************************************************************/

namespace Util
{
    using System.Collections;
    using System.IO;
    using UnityEngine;

    public class CaptureScreenshot : MonoBehaviour
    {
        public static CaptureScreenshot ins;
        private static string CollectPata { get { return PathConfig.Logs; } }

        public static void Init(GameObject go)
        {
            debug.log(Application.persistentDataPath);
            ins = go.AddComponent<CaptureScreenshot>();
        }

        /// <summary>
        /// 保存截屏图片，并且刷新相册（Android和iOS）
        /// </summary>
        /// <param name="name">若空就按照时间命名</param>
        public void CaptureScreenshots(string _name = "")
        {
            if (_name == "")
            {
                _name = TimesUtil.GetCurTime();
            }

            int type = 0;
#if UNITY_EDITOR
            type = 0;
#endif
#if UNITY_EDITOR_WIN
            type = 1;
#endif
#if UNITY_ANDROID
            type = 2;
#endif
            if (type == 0)
            {
                StartCoroutine(EditorCutImage(_name));
            }
            else if (type == 1)
            {
                StartCoroutine(PcCutImage(_name));
            }
            else if (type == 2)
            {
                //Android版本
                StartCoroutine(AndroidCutImage(_name));
                //Debug.Log("图片保存地址" + _name);
            }
        }

        public string Paths(string name)
        {
            string path = string.Format("{0}/{1}", CollectPata, name);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return string.Format("{0}/{1}.png", path, name);
        }

        private IEnumerator EditorCutImage(string name)
        {
            yield return new WaitForEndOfFrame();
            //编辑器下
            string path = Paths(name);
            ScreenCapture.CaptureScreenshot(path);
            //Debug.Log("图片保存地址" + path);
        }

        private IEnumerator PcCutImage(string name)
        {
            //图片大小
            Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
            yield return new WaitForEndOfFrame();
            tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
            tex.Apply();
            yield return tex;
            byte[] byt = tex.EncodeToPNG();

            //编辑器下
            string path = Paths(name);

            //保存截图
            File.WriteAllBytes(path, byt);
        }

        //截屏并保存
        private IEnumerator AndroidCutImage(string name)
        {
            //图片大小
            Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
            yield return new WaitForEndOfFrame();
            tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
            tex.Apply();
            yield return tex;
            byte[] byt = tex.EncodeToPNG();
            string AllPtah = Paths(name);
            File.WriteAllBytes(AllPtah, byt);
        }
    }
}